#include "linkstack.h"

stack *init_stack(void)
{
    stack *s = calloc(1, sizeof(stack));
    if(s != NULL)
    {
        s->top = calloc(1, sizeof(node));
        INIT_LIST_HEAD(&s->top->list);
        s->size = 0;
    }
    return s;
}

list new_node(int data)
{
    list new = calloc(1, sizeof(node));
    if(new != NULL)
    {
        new->data = data;
        INIT_LIST_HEAD(&new->list);
    }
    return new;
}

bool is_empty(stack *s)
{
    return s->size == 0;
}

bool push(stack *s, list newnode)
{
    if(newnode == NULL)
        return false;

    list_add(&newnode->list, &s->top->list);
    s->size++;
    return true;
}

bool pop(stack *s, list *k)
{
    if(is_empty(s))
        return false;

    *k = list_entry(s->top->list.next, node, list);
    list_del_init(&(*k)->list);
    s->size--;
    return true;
}